package com.zj.sms;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.log4j.Logger;

import com.zj.sms.bean.UrlRecordVo;
import com.zj.sms.constant.Constants;
import com.zj.sms.constant.Seed;
import com.zj.sms.http.HttpClient;

/**
 * Hello world!
 *
 */
public class App 
{
	public static long startTime = System.currentTimeMillis();
	static int num = 36;
	private static Logger logger = Logger.getLogger(App.class);
    public static void main( String[] args )
    {
    	//添加程序结束监听 
    	Runtime.getRuntime().addShutdownHook(new ShutDownWork());
        System.out.println( "Hello World!" );
        String content = null;
		try {
			content = HttpClient.getInstance().getContent(new HttpGet(Seed.seed));
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
        if(content == null){System.out.println("获取网页数据失败! url: "+Seed.seed);}
        ReleaseContentUtil.releaseContent(1,Seed.seed,content);
        
        logger.info("开始监控队列数据");
        new Thread(new MonitorJob()).start();
        
        for(int i=0;i<num;i++){
	        new Thread(){
	        	public void run(){
	        		try {
						Thread.sleep(2000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					};
	        		logger.info(Thread.currentThread().getName()+"启动!");
	        		while(Seed.unVisitedQueue.size()>0){
	        			UrlRecordVo uv = Seed.unVisitedQueue.poll();
	        			if(Seed.visitedQueue.contains(uv.getUrl())){
	        				logger.info(uv.getUrl()+"已访问过，跳过!"+Seed.visitedQueue.size());
	        				continue;
	        			}
	        			String key = ReleaseContentUtil.getPreStr(uv.getUrl());
	        			if(Constants.filterList.contains(key)){
	        				logger.info(uv.getUrl()+"包含过滤字符串，跳过!key:"+key);
	        				continue;
	        			}
	        			Seed.visitedQueue.add(uv.getUrl());
	        			String str = null;
	        			long startTime = System.currentTimeMillis();
	        			try{
	        				str = HttpClient.getInstance().getContent(new HttpGet(uv.getUrl()));
	        			}catch(Exception e){
	        				logger.error(uv.getUrl()+"取页面时出错，跳过该页面");
	        				continue;
	        			}
	        			logger.info(uv.getUrl()+"请求耗时秒:"+(System.currentTimeMillis()-startTime)/1000);
	        			if(str == null){
	        				logger.info(uv.getUrl()+"未读取到页面页面数据，跳过!");
	        				continue;
	        			}
	        			ReleaseContentUtil.releaseContent(uv,str);
	        		}
	        	}
	        }.start();
        }
    }
}
